package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysRolePermission;
import cn.wgx.modules.sys.entity.SysRolePermissionExample;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.entity.mapper.SysRolePermissionMapper;
import cn.wgx.modules.sys.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysRolePermissionService extends BaseService<SysRolePermission> {

    @Resource
    SysRolePermissionMapper mapper;

    @Transactional(readOnly = true)
    public List<SysRolePermission> getList(SysRolePermission sysRolePermission) {
        SysRolePermissionExample example = new SysRolePermissionExample();
        if(sysRolePermission != null){
            SysRolePermissionExample.Criteria criteria = example.createCriteria();
            if(Validation.isNotNullOrZero(sysRolePermission.getRoleId())){
                criteria.andRoleIdEqualTo(sysRolePermission.getRoleId());
            }
            if(Validation.isNotNullOrZero(sysRolePermission.getPermissionId())){
                criteria.andPermissionIdEqualTo(sysRolePermission.getPermissionId());
            }
        }
        return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysRolePermission> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysRolePermissionExample example = new SysRolePermissionExample();
            SysRolePermissionExample.Criteria criteria = example.createCriteria();
            criteria.andRole_permission_idIn(uniList);
            List<SysRolePermission> eduCourseAssignmentHis = mapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    @Transactional
    public SysRolePermission insertOne(SysRolePermission record) {
        filter(record, true);
        mapper.insertSelective(record);
        return record;
    }

    @Transactional
    public int updateRolePermission( List<SysRolePermission> sysRolePermissions) {
        SysRolePermissionExample example = new SysRolePermissionExample();
        example.createCriteria().andRoleIdEqualTo(sysRolePermissions.get(0).getRoleId());
        mapper.deleteByExample(example);
        Date date = new Date();
        SysUser sysUser = UserUtil.getUser();
        int r = 0;
        for(SysRolePermission sysRolePermission: sysRolePermissions){
            sysRolePermission.setUpdateDate(date);
            sysRolePermission.setCreateDate(date);
            if(sysUser != null){
                sysRolePermission.setCreateUserId(sysUser.getUser_id());
                sysRolePermission.setUpdateUserId(sysUser.getUser_id());
            }
            filter(sysRolePermission, true);
            r += mapper.insertSelective(sysRolePermission);
        }
        return r;

    }

    @Transactional
    public int updateOne( SysRolePermission record) {
        filter(record, false);
        return mapper.updateByPrimaryKey(record);

    }


    @Transactional
    public int deleteOne(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Transactional
    public int deleteByMenuId(Integer roleId) {
        SysRolePermissionExample example = new SysRolePermissionExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        return mapper.deleteByExample(example);
    }



}